package com.bigdata.quality.utils;

import cn.hutool.core.util.StrUtil;
import com.bigdata.quality.config.converter.ColumnTypeConverter;
import com.bigdata.quality.config.converter.ConverterFactory;
import com.bigdata.quality.config.enums.DatabaseEnums;

import java.util.Map;

/**
 *  数据源 TO Flink 类型映射转换，先获取映射列表、再转换
 */
public class ConverterUtils {

  private static final String flinkDefaultType = "STRING";
  private static final ConverterFactory converterFactory = new ConverterFactory();

  public static ColumnTypeConverter get(DatabaseEnums databaseEnums) {
    return converterFactory.getColumnTypeConverter(databaseEnums);
  }

  public static Map<String, String> toFlinkColumnTypeMap(DatabaseEnums databaseEnums) {
    ColumnTypeConverter converter = get(databaseEnums);
    return converter.toFlinkColumnTypeMap();
  }

  public static String convert2FlinkColumnType(Map<String, String> toFlinkColumnTypeMap, String sourceType) {
    if (StrUtil.isBlank(sourceType)) {
      return flinkDefaultType;
    }

    String destType = toFlinkColumnTypeMap.get(sourceType);
    return StrUtil.isBlank(destType) ? flinkDefaultType : destType;
  }
}
